//
//  Token.swift
//  Maze24
//
//  Created by Mars on 2020/11/14.
//

import Foundation

enum Op: String, CaseIterable {
  case add = "+"
  case mul = "×"
  case sub = "-"
  case div = "÷"
}

extension Op {
  func calculate(l: Int, r: Int) -> Int {
    switch self {
    case .add:
      return l + r
    case .sub:
      return l - r
    case .mul:
      return l * r
    case .div:
      return r != 0 ? l / r : 0
    }
  }
}

enum Token {
  case `operator`(Op)
  case operand(Int)
}

extension Token: Equatable {
  static func == (lhs: Token, rhs: Token) -> Bool {
    if case Token.operator(let l) = lhs, case Token.operator(let r) = rhs {
      return l.rawValue == r.rawValue
    }
    
    if case Token.operand(let l) = lhs, case Token.operand(let r) = rhs {
      return l == r
    }
    
    return false
  }
}
